package main.java.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import main.java.dao.LibraryMapper;
import main.java.dao.MdMapper;
import main.java.entity.InstanceEntity;
import main.java.entity.Library;
import main.java.entity.TreeEntity;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
@Service
public class LibraryService {
	protected final static Log log = LogFactory.getLog(LibraryService.class);

	@Resource
	private LibraryMapper libraryMapper;
	
	@Resource
	private MdMapper mdMapper;

	/**
	* 查询所有分类信息
	* @return
	*/
	public List<TreeEntity> findNodesByParentId(String id) {
		List<TreeEntity> listZTree = new ArrayList<TreeEntity>();
		listZTree = forNodes(id,listZTree);
		return listZTree;
	}
	
	public List<TreeEntity> mdFindNodesByParentId(InstanceEntity paramEntity){
		List<TreeEntity> listZTree = new ArrayList<TreeEntity>();
		listZTree = mdforNodes(paramEntity,listZTree);
		return listZTree;
	}
	
	
	/**
	 * 递归查找树节点
	 * @param id
	 * @param sbf
	 * @param listZTree
	 * @return
	 */
	private List<TreeEntity> mdforNodes(InstanceEntity entityParam,List<TreeEntity> listZTree) {
		entityParam.setId(entityParam.getId().replace("Procedure_", "").replace("SQLIndex_", "").replace("Table_", "").replace("View_", ""));
		if(("Schema").equals(entityParam.getClasf_cd())) {
			//先按模型查询 是否有子节点
			InstanceEntity param=new InstanceEntity();
			param.setClasf_cd("Procedure");
			param.setId(entityParam.getId());
			List<InstanceEntity> list = mdMapper.mdfindNodesByParentId(param);
			if(list.size()>0) {
				TreeEntity tree=new TreeEntity();
				tree.setName("Procedure");
				tree.setIsParent("true");
				//tree.setIcon("");
				tree.setId("Procedure_"+entityParam.getId());
				tree.setClasfCd("Procedure");
				tree.setpId(entityParam.getId());
				tree.setParentTId("tree_"+entityParam.getId());
				tree.settId("tree_"+entityParam.getId());
				listZTree.add(tree);
			}
			param.setClasf_cd("SQLIndex");
			list = mdMapper.mdfindNodesByParentId(param);
			if(list.size()>0) {
				TreeEntity tree=new TreeEntity();
				tree.setName("SQLIndex");
				tree.setIsParent("true");
				tree.setId("SQLIndex_"+entityParam.getId());
				//tree.setIcon("");
				tree.setClasfCd("SQLIndex");
				tree.setpId(entityParam.getId());
				tree.setParentTId("tree_"+entityParam.getId());
				tree.settId("tree_"+entityParam.getId());
				listZTree.add(tree);
			}
			param.setClasf_cd("Table");
			list = mdMapper.mdfindNodesByParentId(param);
			if(list.size()>0) {
				TreeEntity tree=new TreeEntity();
				tree.setName("Table");
				tree.setIsParent("true");
				//tree.setIcon("");
				tree.setId("Table_"+entityParam.getId());
				tree.setClasfCd("Table");
				tree.setpId(entityParam.getId());
				tree.setParentTId("tree_"+entityParam.getId());
				tree.settId("tree_"+entityParam.getId());
				listZTree.add(tree);
			}
			param.setClasf_cd("View");
			list = mdMapper.mdfindNodesByParentId(param);
			if(list.size()>0) {
				TreeEntity tree=new TreeEntity();
				tree.setName("View");
				tree.setIsParent("true");
				//tree.setIcon("");
				tree.setId("View_"+entityParam.getId());
				tree.setClasfCd("View");
				tree.setpId(entityParam.getId());
				tree.setParentTId("tree_"+entityParam.getId());
				tree.settId("tree_"+entityParam.getId());
				listZTree.add(tree);
			}
			return listZTree;
		}
		if(!(("Procedure").equals(entityParam.getClasf_cd()) || ("SQLIndex").equals(entityParam.getClasf_cd()) || ("Table").equals(entityParam.getClasf_cd()) || ("View").equals(entityParam.getClasf_cd()))) {
			entityParam.setClasf_cd("");
		}
		List<InstanceEntity> entityList = mdMapper.mdfindNodesByParentId(entityParam);
		if(null!=entityList && entityList.size()>0) {
			for (int i = 0; i < entityList.size(); i++) {
				InstanceEntity entity = entityList.get(i);
				String name="";
				if(StringUtils.isNotBlank(entity.getInst_nm())) {
					name=entity.getInst_nm().replace("\"", "");
				}else {
					name=entity.getInst_cd().replace("\"", "");
				}
				String showClasfCd=entity.getClasf_cd();
				if(!(("Procedure").equals(entityParam.getClasf_cd()) || ("SQLIndex").equals(entityParam.getClasf_cd()) || ("Table").equals(entityParam.getClasf_cd()) || ("View").equals(entityParam.getClasf_cd()))) {
					entity.setClasf_cd("");
				}
				//是否为parent节点
				List<InstanceEntity> isParentNode = mdMapper.mdfindNodesByParentId(entity);
				TreeEntity tree=new TreeEntity();
				tree.setId(entity.getId());
				tree.setpId(entity.getPar_inst_id());
				tree.setName(name);
				tree.setParentTId("tree_"+entity.getPar_inst_id());
				tree.settId("tree_"+entity.getPar_inst_id());
				tree.setClasfCd(showClasfCd);
				//tree.setIcon(entity.getDisp_icon());
				if(isParentNode.size()>0) {
					tree.setIsParent("true");
				}
				listZTree.add(tree);
//				log.info(str);
			}
		}
		return listZTree;
	}
	
	/**
	 * 递归查找树节点
	 * @param id
	 * @param sbf
	 * @param listZTree
	 * @return
	 */
	private List<TreeEntity> forNodes(String id,List<TreeEntity> listZTree) {
		List<Library> listLibrary = libraryMapper.findNodesByParentId(id);
		if(null!=listLibrary && listLibrary.size()>0) {
			for (int i = 0; i < listLibrary.size(); i++) {
				Library entity = listLibrary.get(i);
				String name=entity.getName();
				//是否为parent节点
				List<Library> isParentNode = libraryMapper.findNodesByParentId(entity.getId());
				TreeEntity tree=new TreeEntity();
				tree.setId(entity.getId());
				tree.setpId(entity.getPid());
				tree.setName(name);
				tree.setParentTId("tree_"+entity.getPid());
				tree.settId("tree_"+entity.getPid());
//				tree.setIcon(entity.getDisp_icon());
				if(isParentNode.size()>0) {
					tree.setIsParent("true");
				}
//				log.info(str);
				listZTree.add(tree);
			}
		}
		return listZTree;
	}
	
	public List<Object> findAllParentNode(){
		List<Object> listZTree = new ArrayList<Object>();
		List<Library> listLibrary = libraryMapper.findAllParentNode();
		String str = "";
		for (int i = 0; i < listLibrary.size(); i++) {
			Library library = listLibrary.get(i);//分类信息
			str = "{\"id\":'" + library.getId() + "', \"tId\":'tree_"+library.getId()+"', \"pId\":'" + library.getPid() + "', \"name\":\"" + library.getName() + "\",\"isParent\":true}";//封装ztree需要格式的字符串
//			log.info(str);
			listZTree.add(str);
		}
		return listZTree;
	}
}
